Components.utils.import("resource://mldonkeyff/MLDonkeyDBConfig.js");
Components.utils.import("resource://mldonkeyff/MLDonkeyUtils.js");

var MLDonkeyPreferencesWindowController = {
	onLoad: function() {
		// Ids
		MLDonkeyPreferencesWindowController.serverListBox = document.getElementById("serverListBox");
		MLDonkeyPreferencesWindowController.addButton = document.getElementById("addButton");
		MLDonkeyPreferencesWindowController.editButton = document.getElementById("editButton");
		MLDonkeyPreferencesWindowController.deleteButton = document.getElementById("deleteButton");
		// Gui initializations
		MLDonkeyPreferencesWindowController.editButton.setAttribute("disabled", true);
		MLDonkeyPreferencesWindowController.deleteButton.setAttribute("disabled", true);
		MLDonkeyPreferencesWindowController.populateServerListBox();
		// Events
		MLDonkeyPreferencesWindowController.serverListBox.addEventListener("click", this.onServerListBoxClick, true);
	},
	onAddServer: function() {
		var serverDataDialog = window.openDialog('chrome://mldonkeyff/content/xul/MLDonkeyServerDataDialog.xul', 'MLDonkeyServerDataDialog', 'chrome', 'add');
		serverDataDialog.addEventListener("beforeunload", MLDonkeyPreferencesWindowController.populateServerListBox, true);
	},
	onEditServer: function() {
		var serverDataDialog = window.openDialog('chrome://mldonkeyff/content/xul/MLDonkeyServerDataDialog.xul', 'MLDonkeyServerDataDialog', 'chrome', 'edit', MLDonkeyPreferencesWindowController.serverListBox.getSelectedItem(0).getUserData("key"));
		serverDataDialog.addEventListener("beforeunload", MLDonkeyPreferencesWindowController.populateServerListBox, true);
	},
	onDeleteServer: function() {
		var confirm = MLDonkeyUtils.getPromptService().confirm(this, "Delete", "Delete?");
		if(confirm) {
			MLDonkeyDBConfig.deleteConfiguredClient(MLDonkeyPreferencesWindowController.serverListBox.getSelectedItem(0).getUserData("key"));
			MLDonkeyPreferencesWindowController.populateServerListBox();
		}
	},
	onServerListBoxClick: function(event) {
//		var msg = "";
//		for(a in this) {
//			msg += a + " = '" + event[a] + "'\r\n";
//		}
//		alert(msg);
		if(event.type == "click") {
			if(MLDonkeyPreferencesWindowController.serverListBox.getSelectedItem(0) != null) {
				MLDonkeyPreferencesWindowController.editButton.setAttribute("disabled", false);
				MLDonkeyPreferencesWindowController.deleteButton.setAttribute("disabled", false);
			} else {
				MLDonkeyPreferencesWindowController.editButton.setAttribute("disabled", true);
				MLDonkeyPreferencesWindowController.deleteButton.setAttribute("disabled", true);
			}
		}
	},
	populateServerListBox: function() {
		// Clean
		for(var i = MLDonkeyPreferencesWindowController.serverListBox.getRowCount(); i >= 0; i--) {
			MLDonkeyPreferencesWindowController.serverListBox.removeItemAt(i);
		}
		// Populate
		for(clientId in MLDonkeyDBConfig.getConfiguredClients()) {
			var client = MLDonkeyDBConfig.getConfiguredClients()[clientId];
			var row = document.createElement('listitem');
			var cell = null;
			// Hostname
			cell = document.createElement('listcell');
			cell.setAttribute('label', client.getHostname());
			row.appendChild(cell);
			// Port
			cell = document.createElement('listcell');
			cell.setAttribute('label', client.getPort());
			row.appendChild(cell);
			// Username
			cell = document.createElement('listcell');
			cell.setAttribute('label', client.getUsername());
			row.appendChild(cell);
			//
			row.setUserData("key", clientId, null);
			//
			MLDonkeyPreferencesWindowController.serverListBox.appendChild(row);
		}
	},
}